package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.Brochure;
	import com.ettractions.kiosk.components.BrochureMapDirectionsView;
	import com.ettractions.kiosk.events.BrochureSelectedEvent;
	import com.ettractions.kiosk.models.KioskModel;
	import com.ettractions.kiosk.vo.BrochureVO;
	import com.google.maps.Color;
	import com.google.maps.LatLng;
	import com.google.maps.MapEvent;
	import com.google.maps.MapType;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.services.Directions;
	import com.google.maps.services.DirectionsEvent;
	
	import flash.geom.Point;
	
	import flashx.textLayout.conversion.TextConverter;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class BrochureMapDirectionsMediator extends Mediator
	{
		private static const GOOGLE_MAPS_API_KEY:String = "ABQIAAAAOYcjh4KTljlE4n2lrQ7qyBQWdVyIqRmenvXTsenL4e2EQFZVvBQICD9paKAWP1dgwjEqv3Va54E-FA";
		
		[Inject]
		public var model:KioskModel;
		
		[Inject]
		public var view:BrochureMapDirectionsView;
		
		[Embed(source="../../../../../png/CurrentLocationIcon.png")]
		public var CurrentLocationIcon:Class;
		
		[Embed(source="../../../../../png/AttractionLocationIcon.png")]
		public var AttractionLocationIcon:Class;
		
		private var _directions:Directions;
		
		private var _currentBrochure:BrochureVO = null;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(BrochureSelectedEvent.BROCHURE_SELECTED, handleBrochureSelected);
			
			view.map.key = GOOGLE_MAPS_API_KEY;
			view.map.addEventListener(MapEvent.MAP_READY, handleMapReady);
		}
		
		private function handleBrochureSelected(event:BrochureSelectedEvent):void
		{
			_currentBrochure = event.brochure;
			
			if (_currentBrochure != null)
			{
				view.data = _currentBrochure;
				
				_directions.loadFromWaypoints([
					new LatLng(model.location.latitude, model.location.longitude),
					new LatLng(event.brochure.location.latitude, event.brochure.location.longitude)
				]);
			}
		}
		
		private function handleMapReady(event:MapEvent):void
		{
			view.map.removeEventListener(MapEvent.MAP_READY, handleMapReady);
			
			_directions = new Directions();
			_directions.addEventListener(DirectionsEvent.DIRECTIONS_SUCCESS, handleDirectionsSuccess);
			
			if (_currentBrochure != null)
				_directions.loadFromWaypoints([
					new LatLng(model.location.latitude, model.location.longitude),
					new LatLng(_currentBrochure.location.latitude, _currentBrochure.location.longitude)
				]);
		}
		
		private function handleDirectionsSuccess(event:DirectionsEvent):void
		{
			var copy:String = "<b>Distance:</b><br />"
			                + _directions.distanceHtml + "<br />"
			                + "<br />"
			                + "<b>Transportation:</b><br />";
			
			for (var i:uint = 0; i < _directions.getRoute(0).numSteps; i++)
				copy += _directions.getRoute(0).getStep(i).descriptionHtml + ".  ";
			
			copy += "<br /><br />"
				 + "<b>Location:</b><br />"
			     + _currentBrochure.location.fullAddress.replace("\n", "<br />");
			
			view.copy.textFlow = TextConverter.importToFlow(copy, TextConverter.TEXT_FIELD_HTML_FORMAT);
			
			view.map.clearOverlays();
			view.map.addOverlay(_directions.createPolyline());
			view.map.setZoom(view.map.getBoundsZoomLevel(_directions.bounds));
			view.map.setCenter(_directions.bounds.getCenter());
			view.map.addOverlay(
				new Marker(
					_directions.getRoute(0).startGeocode.point,
					new MarkerOptions({
						icon:new CurrentLocationIcon(),
						iconAlignment:MarkerOptions.ALIGN_HORIZONTAL_CENTER,
						iconOffset:new Point(0, -10)
					})
				)
			);
			view.map.addOverlay(
				new Marker(
					_directions.getRoute(0).endGeocode.point,
					new MarkerOptions({
						icon:new AttractionLocationIcon(),
						iconAlignment:MarkerOptions.ALIGN_HORIZONTAL_CENTER,
						iconOffset:new Point(0, -16)
					})
				)
			);
		}
	}
}